<template>
  <page-header-wrapper :title="false">
    <a-card title="所有业务">
      <template slot="extra">
        <a-button style="margin-right:10px" size="small" icon="plus" type="primary" @click="$router.push({ name: 'AddBusiness' })">添加</a-button>
        <a-button size="small" icon="redo" type="primary" @click="resetForm">刷新</a-button>
      </template>
      <div class="table-page-search-wrapper">
        <a-form-model
          layout="inline"
          :model="queryParam"
        >
          <a-row :gutter="15">
            <a-col :md="6" :sm="24">
              <a-form-model-item label="业务标识">
                <a-input v-model="queryParam['mark']" placeholder="请输入业务标识"/>
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-model-item label="业务名称">
                <a-input v-model="queryParam['name']" placeholder="请输入业务名称"/>
              </a-form-model-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="6" :sm="24">
                <a-form-item label="所在组别">
                  <a-select v-model="queryParam['cat_group']" placeholder="请选择组别">
                    <a-select-option v-for="(item,index) in groupOpts" :key="index" :value="index">
                      {{ item }}
                    </a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="是否显示">
                  <a-select v-model="queryParam['hide']" placeholder="请选择是否显示">
                    <a-select-option :value="0">是</a-select-option>
                    <a-select-option :value="1">否</a-select-option>
                  </a-select>
                </a-form-model-item>
              </a-col>
            </template>
            <a-col :md="!advanced && 8 || 24" :sm="24">
              <span class='table-page-search-submitButtons' :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
                <a-button type="primary" @click="$refs.table.refresh(true)">搜索</a-button>
                <a-button style="margin-left: 8px" @click="resetForm">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form-model>
      </div>
      <div class="table-operator">
        <s-table
          bordered
          ref="table"
          :row-key="(record) => record.business_id"
          :columns="priceColumns"
          :data="priceData"
        >
          <div slot="title">
            <a-button size="small" icon="save" type="primary" @click="onBatch">保存修改</a-button>
          </div>
          <template slot="mark" slot-scope="text">
            {{text}}
          </template>
          <template slot="sort" slot-scope="text,record">
            <a-input type="text" style="width: 80px" v-model="sortForm['px' + record.business_id]" />
          </template>
          <template slot="name" slot-scope="text">
            {{text}}
          </template>
          <template slot="cat_group" slot-scope="text">
            <div v-for="(item,index) in groupOpts" :key="index" :value="index">
              <div v-if="index == text">{{item}}</div>
            </div>
          </template>
          <template slot="official_fee" slot-scope="text,record">
            <a-input v-model="offForm['off' + record.business_id]" style="width:100px" suffix="元" type="text" />
          </template>
          <template slot="vip_offer_section_lft" slot-scope="text,record">
            <a-input v-model="vipForm['vip' + record.business_id]" style="width:100px" suffix="元" type="text" />
          </template>
          <template slot="offer_section_lft" slot-scope="text,record">
            <a-input v-model="offzForm['offz' + record.business_id]" style="width:100px" suffix="元" type="text" />
          </template>
          <template slot="offer_section_rgt" slot-scope="text,record">
            <a-input v-model="vipzForm['vipz' + record.business_id]" style="width:100px" suffix="元" type="text" />
          </template>
          <template slot="hide" slot-scope="text">
            <a-tag color="red" v-if="text == 0">显示</a-tag>
            <a-tag v-else>隐藏</a-tag>
          </template>
          <template slot="action" slot-scope="text,record">
            <a-button type="primary" size="small" @click="$router.push({ name: 'EditBusiness', params: { id: record.business_id } })">编辑</a-button>
          </template>
        </s-table>
      </div>
    </a-card>
  </page-header-wrapper>
</template>

<script>
  import { STable } from '@/components'
  import priceColumns from '@/columns/business'
  import { getPatentBusiness, postEditBusiness, postPatentBusiness } from '@/api/business'

  export default {
    components: {
      STable
    },
    name: 'PgBusiness',
    data () {
      return {
        priceColumns: priceColumns.price,
        priceData: parameters => {
          return getPatentBusiness(Object.assign(parameters, this.queryParam)).then(res => {
            this.groupOpts = res.result.categoryGroupOpts
            for (let i = 0; i < res.result.data.length; i++) {
              this.offForm['off' + res.result.data[i].business_id] = res.result.data[i].official_fee
              this.vipForm['vip' + res.result.data[i].business_id] = res.result.data[i].vip_offer_section_lft
              this.sortForm['px' + res.result.data[i].business_id] = res.result.data[i].sort
              this.offzForm['offz' + res.result.data[i].business_id] = res.result.data[i].offer_section_lft
              this.vipzForm['vipz' + res.result.data[i].business_id] = res.result.data[i].offer_section_rgt
            }
            return res.result
          })
        },
        advanced: true,
        groupOpts: {},
        queryParam: {},
        sortForm: {},
        offForm: {},
        vipForm: {},
        offzForm: {},
        vipzForm: {}
      }
    },
    created () {
      this.queryParam['category_id'] = '70'
    },
    methods: {
      onBatch () {
        const app = this
        const data = {}
        for (const k in app.offForm) {
          data['values[' + k.match(/\d+/g) + '][official_fee]'] = app.offForm[k]
        }
        for (const k in app.vipForm) {
          data['values[' + k.match(/\d+/g) + '][vip_offer_section_lft]'] = app.vipForm[k]
        }
        for (const k in app.sortForm) {
          data['values[' + k.match(/\d+/g) + '][sort]'] = app.sortForm[k]
        }
        for (const k in app.offzForm) {
          data['values[' + k.match(/\d+/g) + '][offer_section_lft]'] = app.offzForm[k]
        }
        for (const k in app.vipzForm) {
          data['values[' + k.match(/\d+/g) + '][offer_section_rgt]'] = app.vipzForm[k]
        }
        postPatentBusiness(data).then(res => {
          if (res.status === 0) {
            app.$refs.table.refresh()
          } else {
            app.$warning({
              title: res.msg
            })
          }
        })
      },
      onSetting (id, source, type) {
        const app = this
        const data = {}
        if (type === 'official_fee') {
          data['Business[' + type + ']'] = app.offForm['off' + id]
        }
        if (type === 'vip_offer_section_lft') {
          data['Business[' + type + ']'] = app.vipForm['vip' + id]
        }
        if (type === 'sort') {
          data['Business[' + type + ']'] = app.sortForm['px' + id]
        }
        postEditBusiness(id, data).then(res => {
          if (res.status === 0) {
            app.$refs.table.refresh()
          } else {
            app.$warning({
              title: res.msg
            })
          }
        })
      },
      toggleAdvanced () {
        this.advanced = !this.advanced
      },
      resetForm () {
        this.queryParam = {}
        this.queryParam['category_id'] = '70'
        this.$refs.table.refresh(true)
      }
    }
  }
</script>

<style scoped>

</style>
